// -*- Java -*-
/*
 * <copyright>
 * 
 *  Copyright (c) 2002
 *  Institute for Information Processing and Computer Supported New Media (IICM),
 *  Graz University of Technology, Austria.
 * 
 * </copyright>
 * 
 * <file>
 * 
 *  Name:    Input.java
 * 
 *  Purpose: Input reads and parses the KWIC input file
 * 
 *  Created: 20 Sep 2002 
 * 
 *  $Id$
 * 
 *  Description:
 *    Input reads and parses the KWIC input file
 * </file>
 */

package kwic.oo;

/*
 * $Log$
 */

import java.util.Scanner;
import java.util.StringTokenizer;

/**
 * Input class is responsible for reading and parsing the content of a KWIC
 * input file. The format of the KWIC input file is as follows:
 * <ul>
 * <li>Lines are separated by the line separator character(s) (on Unix '\n', on
 * Windows '\r\n')
 * <li>Each line consists of a number of words. Words are delimited by any
 * number and combination of the space chracter (' ') and the horizontal
 * tabulation chracter ('\t').
 * </ul>
 * Data is parsed and stored in memory as an instance of the LineStorage class:
 * <ul>
 * <li>All line separators are removed from the data; for each new line in the
 * file a new line is added to the LineStorage instance
 * <li>All horizontal tabulation word delimiters are removed
 * <li>All space character word delimiters are removed
 * <li>From characters between any two word delimiters a new string is created;
 * the new string is added as a word to a particular line.
 * </ul>
 * 
 * @author dhelic
 * @version $Id$
 */

public class Input {

	private Scanner in = new Scanner(System.in);
	private CircularShifter shifter;

	public Input(CircularShifter shifter) {
		this.shifter = shifter;
	}

	/**
	 * This method reads and parses a KWIC input file. If an I/O exception
	 * occurs during the execution of this method, an error message is shown and
	 * program exits.
	 * 
	 * @param file
	 *            name of KWIC input file
	 * @param line_storage
	 *            holds the parsed data
	 */

	public void parse(String line, LineStorage line_storage) {
		StringTokenizer tokenizer = new StringTokenizer(line); // " \t\n\r\f"
		// are
		// delimiter
		// character
		if (tokenizer.countTokens() > 0)
			line_storage.addEmptyLine();
		while (tokenizer.hasMoreTokens())
			line_storage.addWord(tokenizer.nextToken(), line_storage
					.getLineCount() - 1);
	}

	/**
	 * Read the command char.
	 * 
	 * @return If there is an error, return -1
	 */
	public char readCmd() {
		String cmd = in.nextLine();
		if (cmd == null || cmd.length() <= 0)
			return (char) -1;
		return cmd.trim().toLowerCase().charAt(0);
	}

	public void readLine() {
		String line = in.nextLine().trim();
		this.shifter.makeShifts(line);
	}
}
